package com.lionxing.DNTG;

import java.util.HashMap;

import org.cocos2d.events.CCTouchDispatcher;
import org.cocos2d.layers.CCLayer;
import org.cocos2d.layers.CCScene;
import org.cocos2d.menus.CCMenu;
import org.cocos2d.menus.CCMenuItemImage;
import org.cocos2d.nodes.CCDirector;
import org.cocos2d.nodes.CCSprite;
import org.cocos2d.types.CGPoint;
import org.cocos2d.types.CGSize;

import android.view.MotionEvent;

public class SettingLayer extends CCLayer {
	BarSpirte spBar;
	BarSpirte spBar2;
	public static CCScene scene() {
		CCScene scene = CCScene.node();
		SettingLayer layer = new SettingLayer();

		scene.addChild(layer);
		
		return scene;
	}
	
	
	protected SettingLayer()
	{
		CGSize size = CCDirector.sharedDirector().winSize();
		CCSprite bgSp = CCSprite.sprite("bg.png");
		
		
		bgSp.setScaleX((size.width/GameDefine.KIos_W));
		bgSp.setScaleY((size.height/GameDefine.KIos_h));
		bgSp.setPosition(CGPoint.make(size.width/2, size.height/2));
		
		addChild(bgSp,-1);
				
		
		spBar = BarSpirte.sprite("option1.png");
		spBar.setSptype(0);
		
		spBar.setPosition(CGPoint.make(160, 320));
		
		spBar2 = BarSpirte.sprite("option2.png");
		spBar2.setSptype(1);
		
		spBar2.setPosition(CGPoint.make(160, 160));
		
		bgSp.addChild(spBar);
		bgSp.addChild(spBar2);
		
//
//        CCMenuItemImage* menuitemback = [CCMenuItemImage itemFromNormalImage:@"back.png" selectedImage:@"back2.png" target:self selector:@selector(backToMain:)];
//        CCMenu* menu = [CCMenu menuWithItems:menuitemback, nil];
//        menu.position = ccp(267, 55);
		
		HashMap<String, Object> tmpHash = filemanager.sharedDirector().getHashmapgameinfo();
		
		if(tmpHash.get("bgsound") == null)
		{
			spBar.setPercent(50.0f);
		}
		else
		{
			float tmpfloat = Float.parseFloat(tmpHash.get("bgsound").toString());
			spBar.setPercent(tmpfloat);
			
		}
		
		if(tmpHash.get("effectsound") == null)
		{
			spBar2.setPercent(50.0f);
		}
		else
		{
			float tmpfloat = Float.parseFloat(tmpHash.get("effectsound").toString());
			spBar2.setPercent(tmpfloat);
		}
		
		CCMenuItemImage menuitemback = CCMenuItemImage.item("back.png", "back2.png",this, "gotoBack");
		CCMenu menuBack = CCMenu.menu(menuitemback);
		menuBack.setPosition(CGPoint.make(267, 55));
		bgSp.addChild(menuBack);
		
		
	}
	
	
	public void gotoBack(Object sender)
	{
		playSound.sharedDirector().playEffectSound(R.raw.kick);
		HashMap<String, Object> tmpHash = filemanager.sharedDirector().getHashmapgameinfo();
		
		tmpHash.put("bgsound", spBar.getPercent());
		tmpHash.put("effectsound", spBar2.getPercent());
		
		filemanager.sharedDirector().savegameinfo();
		
		CCScene scene = MainLayer.scene();
		// CCDirector.sharedDirector().replaceScene(scene);
		 scenemanager.go(scene);
	}

	
	 public  void onExit()
		{		
				this.removeAllChildren(true);
				super.onExit();
				
				spBar = null;
				spBar2 = null;
		}

}
